package com.vsd.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.dialet.PaginationSupport;
import com.demo.domain.Message;
import com.demo.util.GetuiPushUtil;
import com.demo.util.JSONUtils;
import com.demo.util.ProperityUtil;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.vsd.daoEx.model.MessageEx;
import com.vsd.service.MessageService;

@Controller
@RequestMapping("message")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class MessageController extends CoreAdminController{
	@Autowired
	private MessageService messageService;
	@Autowired
	ProperityUtil properityUtil;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		
		PaginationSupport.byPage(start/length, length);
		List<MessageEx> list = messageService.selectExList(areaCode,new Sort(Direction.DESC, "createTime"));
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(Message f,HttpSession session,HttpServletRequest request){
		if(StringUtils.isBlank(f.getId())){
			//是否已推荐
			f.setId(StringUtil.getUUID());
			f.setCreateTime(new Date());
			f.setRedirectType(1);
			f.setState(1);
			f.setNum(0);
			messageService.save(f);
		}else{
			messageService.updateByPrimaryKeySelective(f);
		}
		
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id){
		Message r = messageService.findOne(id);
		return JSONUtils.serialize(r);
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String string : ids) {
			messageService.delById(string);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/send")
	@ResponseBody
	public String send(String id){
		if(StringUtils.isBlank(properityUtil.getGetui_appId())){
			return JSONUtils.serialize(new Ret(1,"系统尚未开启推送服务！"));
		}
		
		Message message = messageService.findOne(id);
		if(message==null){
			return JSONUtils.serialize(new Ret(1,"消息不存在"));
		}
		
		if(message.getState() == 2){
			return JSONUtils.serialize(new Ret(1,"消息已被推送过了~"));
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("redirectType", 1);
		params.put("msg_android", message.getContent());
		String ret = "";
		if(message.getType() == 1){
			if(StringUtils.isBlank(message.getAreaCode())) ret = GetuiPushUtil.pushMessageToApp(params, message.getContent(), null, null);
			else ret = GetuiPushUtil.pushMessageToApp(params, message.getContent(),message.getAreaCode(),null);
		}else if(message.getType() == 2){
			ret = GetuiPushUtil.pushToSingle(params, message.getContent(), message.getTarget());
		}
		
		if(StringUtils.isBlank(ret)){
			return JSONUtils.serialize(new Ret(1,"推送失败，请联系开发人员"));
		}
		
		if(ret.contains("result=ok")){
			message.setState(2);
			messageService.updateByPrimaryKeySelective(message);
		}else{
			return JSONUtils.serialize(new Ret(1,"推送失败,返回值："+ret));
		}
		System.out.println(ret);
		return JSONUtils.serialize(new Ret(0));
	}
	
}
